package com.zhenzhen.xszhcp.serviceImpl;

import com.zhenzhen.xszhcp.service.ImportUtilService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ImportUtilServiceImpl implements ImportUtilService {
    public List loadCcsbFromExcel(InputStream file, int a) {
        List list = new ArrayList();
        //解析excel表格
        try {
            Workbook wb = WorkbookFactory.create(file);//创建工作簿
            Sheet sheet = wb.getSheetAt(0);//根据序号获取第几个工作表
            int rowCount = sheet.getLastRowNum() + 1;//获取行索引的结束位置
            boolean isMerge = false;
            for (int ri = a; ri < rowCount; ri++) {//循环得到工作表中所有行对象
                Row row = sheet.getRow(ri);//得到一行的数据
                Map rowMap = new HashMap();
                int zdl = 0;
                boolean isMerge1 = isMergedRegion(sheet, ri, 0);//判断单元格是否是合并单元格
                for (Cell c : row) {//循环这一行，得到这一行中每个单元格的数据
                    isMerge = isMergedRegion(sheet, ri, c.getColumnIndex());
                    //判断是否具有合并单元格
                    String val = "";
                    if (isMerge) {
                        val = getMergedRegionValue(sheet, row.getRowNum(), c.getColumnIndex());//获取单元格的值
                    } else {
                        val = getCellValue(c);//如果没有合并单元和，获取单元格的value值
                    }
                    rowMap.put(c.getColumnIndex(), val);
                }
                if (rowMap != null) {
                    list.add(rowMap);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 获取单元格的值
     *
     * @param cell
     * @return
     */
    public String getCellValue(Cell cell) {

        if (cell == null) return "";

        if (cell.getCellType() == Cell.CELL_TYPE_STRING) {

            return cell.getStringCellValue();

        } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {

            return String.valueOf(cell.getBooleanCellValue());

        } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {

            return cell.getCellFormula();

        } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {

            DecimalFormat df = new DecimalFormat("#");
            return df.format(cell.getNumericCellValue());

        }
        return "";
    }

    /**
     * 获取合并单元格的值
     *
     * @param sheet
     * @param row
     * @param column
     * @return
     */
    public String getMergedRegionValue(Sheet sheet, int row, int column) {

        int sheetMergeCount = sheet.getNumMergedRegions();

        for (int i = 0; i < sheetMergeCount; i++) {
            CellRangeAddress ca = sheet.getMergedRegion(i);
            int firstColumn = ca.getFirstColumn();
            int lastColumn = ca.getLastColumn();
            int firstRow = ca.getFirstRow();
            int lastRow = ca.getLastRow();

            if (row >= firstRow && row <= lastRow) {

                if (column >= firstColumn && column <= lastColumn) {
                    Row fRow = sheet.getRow(firstRow);
                    Cell fCell = fRow.getCell(firstColumn);
                    return getCellValue(fCell);
                }
            }
        }

        return null;
    }

    /**
     * 判断指定的单元格是否是合并单元格
     *
     * @param sheet
     * @param row    行下标
     * @param column 列下标
     * @return
     */
    public boolean isMergedRegion(Sheet sheet, int row, int column) {

        int sheetMergeCount = sheet.getNumMergedRegions();
        for (int i = 0; i < sheetMergeCount; i++) {

            CellRangeAddress range = sheet.getMergedRegion(i);
            int firstColumn = range.getFirstColumn();
            int lastColumn = range.getLastColumn();
            int firstRow = range.getFirstRow();
            int lastRow = range.getLastRow();
            if (row >= firstRow && row <= lastRow) {
                if (column >= firstColumn && column <= lastColumn) {
                    return true;
                }
            }
        }
        return false;
    }
}
